package com.qf.myshop.controller;

import com.qf.myshop.entity.PageBean;
import com.qf.myshop.entity.Product;
import com.qf.myshop.service.ProductService;
import com.qf.myshop.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/product.do")
public class ProductServlet extends BaseServlet {
    private ProductService productService = new ProductServiceImpl();
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String tid = request.getParameter("tid");
        int ntid = Integer.parseInt(tid);
        String page = request.getParameter("page");
        if (page == null || page.trim().equals("")){
            page = "1";
        }
        int npage = Integer.parseInt(page);
        long count = productService.count(ntid);
        List<Product> list = productService.findAll(ntid, npage);
        PageBean pageBean = new PageBean(count, list, npage);
        request.setAttribute("pageBean", pageBean);
        request.getRequestDispatcher("goodsList.jsp").forward(request, response);
    }

    public void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pid = request.getParameter("pid");
        int npid = Integer.parseInt(pid);
        Product product = productService.findById(npid);
        request.setAttribute("product", product);
        request.getRequestDispatcher("goodsDetail.jsp").forward(request, response);
    }
}
